const cjs = {};
// ===================================================================================================
// GUANGZAN                                        
// ===================================================================================================
/**
 * @description: 把手机号处理成155****2509的格式
 * @param {str or number} 
 * @return: new tel
 */
cjs.hidePhoneNumber = function (tel) {
    tel = "" + tel;
    var telRes = tel.substr(0, 3) + "****" + tel.substr(7)
    return telRes
}
/**
 * @description: 日期 - 转 /
 * @param {eg:   '2019-01-03 09:02:01'}
 * @return: 
 */
cjs.dataFormat = function (data) {
    return data.replace(/\-/g, "/");
}
/**
 * @description: 截取等号后面的字符串
 * @param {str}
 * @return: str
 */
cjs.getCaption = function (str) {
    var index = str.lastIndexOf("\=");
    str = str.substring(index + 1, str.length);
    return str;
}
/**
 * @description: 随机生成字符串
 * @param {num}
 * @return: str
 */
cjs.randomKey = function (len) {
    len = len || 32;
    var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'; // 默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1
    var maxPos = $chars.length;
    var key = '';
    for (let i = 0; i < len; i++) {
        key += $chars.charAt(Math.floor(Math.random() * maxPos));
    }
    return key;
}
cjs.secretkey = function (key) {
    let keyS = md5(key);
    // return keyS
    let keySs = keyS.substring(0, 10);
    // return keySs
    let secretkey = md5(keySs);
    return secretkey;
};
/**
 * @description:生成key和secretkey
 * @param {num} key的长度
 * @return: [key, secretkey];
 */
cjs.doubleKeys = function (len) {
    len = len || 32;
    var $chars =
        "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678";
    var maxPos = $chars.length;
    var key = "";
    for (let i = 0; i < len; i++) {
        key += $chars.charAt(Math.floor(Math.random() * maxPos));
    }
    let keyS = md5(key);
    let keySs = keyS.substring(0, 10);
    let secretkey = md5(keySs);
    return [key, secretkey];
}
// 表单验证
/**
 * @description: 判断是否为姓名,已包含少数民族
 * @param {str} 
 * @return: true or false
 */
cjs.isName = function (name) {
    var reg = /^[\u4E00-\u9FA5\uf900-\ufa2d·s]{2,20}$/;
    return reg.test(name);
}
/**
 * @description: 判断是否为空字符串
 * @param {str}
 * @return: true or false
 */
cjs.isEmptyStr = function (str) {
    if (typeof str == "undefined" || str == null || str == "") {
        return true;
    } else {
        return false;
    }
}
/**
 * @description: 判断是否为手机号
 * @param {str}
 * @return: true or false
 */
cjs.isPhoneNumber = function (tel) {
    var reg = /^0?1[3|4|5|6|7|8][0-9]\d{8}$/;
    return reg.test(tel);
}
// 日期处理
/**
 * @returns 今天的日期
 */
cjs.getTody = function () {
    var day2 = new Date();
    day2.setTime(day2.getTime());
    var s2 =
        day2.getFullYear() +
        "-" +
        (day2.getMonth() + 1 < 10 ? "0" : "") +
        (day2.getMonth() + 1) +
        "-" +
        (day2.getDate() <= 10 ? "0" : "") +
        day2.getDate();
    return s2;
};

/**
 * @returns 明天的日期
 */
cjs.getTomorrow = function () {
    var day3 = new Date();
    day3.setTime(day3.getTime() + 24 * 60 * 60 * 1000);
    var s3 =
        day3.getFullYear() +
        "-" +
        (day3.getMonth() + 1 < 10 ? "0" : "") +
        (day3.getMonth() + 1) +
        "-" +
        (day3.getDate() <= 10 ? "0" : "") +
        day3.getDate();
    return s3;
};
// ===================================================================================================
// MINGCHEN                                        
// ===================================================================================================
/**
 * @description:将年月日转化为横杆
 * @prams '2019年2月4日'
 * @returns 2019-2-4
 */
cjs.formatDate = function (date) {
    var dateArr = date.split("");
    dateArr.pop();
    for (var i = 0; i <= dateArr.length; i++) {
        if (i == 4 || i == 7) {
            dateArr[i] = "-";
        }
    }
    return dateArr.join("");
};
/**
 * @returns 相差的天数
 */
cjs.dayCount = function (date1, date2) {
    if (date1 == date2) {
        return 1;
    } else if (date1 && date2) {
        var time1 = Date.parse(date1);
        var time2 = Date.parse(date2);
        var dayCount = Math.abs(time2 - time1) / 1000 / 60 / 60 / 24;
        return dayCount;
    } else {
        return 1;
    }
};
/**
 * 判断字符是否为空的方法
 * @param {*}  ''
 * @return true fasle
 */
cjs.isEmpty = function (obj) {
    if (typeof obj == "undefined" || obj == null || obj == "") {
        return true;
    } else {
        return false;
    }
};

/**
 * 克隆数组
 * @param {*} obj
 * @returns
 */
cjs.deepcopy = function (obj) {
    var out = [],
        i = 0,
        len = obj.length;
    for (; i < len; i++) {
        if (obj[i] instanceof Array) {
            out[i] = deepcopy(obj[i]);
        } else out[i] = obj[i];
    }
    return out;
};
// ***************************************处理时间日期***************************
cjs.timeStyleOne = function (time) {
    let now = new Date(time);
    let Y = now.getFullYear();
    let M =
        now.getMonth() + 1 < 10 ? `0${now.getMonth() + 1}` : now.getMonth() + 1;
    let D = now.getDate() < 10 ? "0" + now.getDate() : now.getDate();
    let H = now.getHours() < 10 ? "0" + now.getHours() : now.getHours();
    let Min = now.getMinutes() < 10 ? "0" + now.getMinutes() : now.getMinutes();
    let Sec = now.getSeconds() < 10 ? "0" + now.getSeconds() : now.getSeconds();
    return `${Y}年${M}月${D}日${H}时${Min}分${Sec}秒`;
};

/* 
  res:2019-3-12 09:48:14
*/
cjs.timeStyleOnes = function (time) {
    if (typeof time == "number") {
        time = parseInt(time) * 1000;
    }
    let now = new Date(time);
    let Y = now.getFullYear();
    let M =
        now.getMonth() + 1 < 10 ? `0${now.getMonth() + 1}` : now.getMonth() + 1;
    let D = now.getDate() < 10 ? "0" + now.getDate() : now.getDate();
    let H = now.getHours() < 10 ? "0" + now.getHours() : now.getHours();
    let Min = now.getMinutes() < 10 ? "0" + now.getMinutes() : now.getMinutes();
    let Sec = now.getSeconds() < 10 ? "0" + now.getSeconds() : now.getSeconds();
    return `${Y}-${M}-${D} ${H}:${Min}:${Sec}`;
};

/* 
  res ： 2019年03月07日15时16分
*/
cjs.timeStyleTwo = function (time) {
    let now = new Date(time);
    let Y = now.getFullYear();
    let M =
        now.getMonth() + 1 < 10 ? `0${now.getMonth() + 1}` : now.getMonth() + 1;
    let D = now.getDate() < 10 ? "0" + now.getDate() : now.getDate();
    let H = now.getHours() < 10 ? "0" + now.getHours() : now.getHours();
    let Min = now.getMinutes() < 10 ? "0" + now.getMinutes() : now.getMinutes();
    return `${Y}年${M}月${D}日${H}时${Min}分`;
};

/* 
  res ： 2019年03月07日 15时16分
*/
cjs.timeStyleThree = function (time) {
    let now = new Date(time);
    let Y = now.getFullYear();
    let M =
        now.getMonth() + 1 < 10 ? `0${now.getMonth() + 1}` : now.getMonth() + 1;
    let D = now.getDate() < 10 ? "0" + now.getDate() : now.getDate();
    let H = now.getHours() < 10 ? "0" + now.getHours() : now.getHours();
    let Min = now.getMinutes() < 10 ? "0" + now.getMinutes() : now.getMinutes();
    return `${Y}年${M}月${D}日 ${H}时${Min}分`;
};

/* 
  res:2019.03.07 15:16
*/
cjs.timeStyleFour = function (time) {
    let now = new Date(time);
    let Y = now.getFullYear();
    let M =
        now.getMonth() + 1 < 10 ? `0${now.getMonth() + 1}` : now.getMonth() + 1;
    let D = now.getDate() < 10 ? "0" + now.getDate() : now.getDate();
    let H = now.getHours() < 10 ? "0" + now.getHours() : now.getHours();
    let Min = now.getMinutes() < 10 ? "0" + now.getMinutes() : now.getMinutes();
    return `${Y}.${M}.${D} ${H}:${Min}`;
};

/* 
  res:2019-03-07
*/
cjs.timeStyleFours = function (time) {
    if (typeof time == "number") {
        time = parseInt(time) * 1000;
    }
    let now = new Date(time);
    let Y = now.getFullYear();
    let M =
        now.getMonth() + 1 < 10 ? `0${now.getMonth() + 1}` : now.getMonth() + 1;
    let D = now.getDate() < 10 ? "0" + now.getDate() : now.getDate();
    let H = now.getHours() < 10 ? "0" + now.getHours() : now.getHours();
    let Min = now.getMinutes() < 10 ? "0" + now.getMinutes() : now.getMinutes();
    return `${Y}-${M}-${D}`;
};

/* 
  res:2019.03.07
*/
cjs.timeStyleFive = function (time) {
    let now = new Date(time);
    let Y = now.getFullYear();
    let M =
        now.getMonth() + 1 < 10 ? `0${now.getMonth() + 1}` : now.getMonth() + 1;
    let D = now.getDate() < 10 ? "0" + now.getDate() : now.getDate();
    return `${Y}.${M}.${D}`;
};

/* 
  res:2019年03月07日
*/
cjs.timeStyleFives = function (time) {
    let now = new Date(time);
    let Y = now.getFullYear();
    let M =
        now.getMonth() + 1 < 10 ? `0${now.getMonth() + 1}` : now.getMonth() + 1;
    let D = now.getDate() < 10 ? "0" + now.getDate() : now.getDate();
    return `${Y}年${M}月${D}日`;
};

/* 
  res:3月7日 15：16
*/
cjs.timeStyleSix = function (time) {
    if (typeof time == "number") {
        time = parseInt(time) * 1000;
    }
    let now = new Date(time);
    let M =
        now.getMonth() + 1 < 10 ? `0${now.getMonth() + 1}` : now.getMonth() + 1;
    let D = now.getDate() < 10 ? "0" + now.getDate() : now.getDate();
    let H = now.getHours() < 10 ? "0" + now.getHours() : now.getHours();
    let Min = now.getMinutes() < 10 ? "0" + now.getMinutes() : now.getMinutes();
    return `${M}月${D}日 ${H}:${Min}`;
};

/* 
  res:03-07 15：16
*/
cjs.timeStyleSixs = function (time) {
    if (typeof time == "number") {
        time = parseInt(time) * 1000;
    }
    let now = new Date(time);
    let M =
        now.getMonth() + 1 < 10 ? `0${now.getMonth() + 1}` : now.getMonth() + 1;
    let D = now.getDate() < 10 ? "0" + now.getDate() : now.getDate();
    let H = now.getHours() < 10 ? "0" + now.getHours() : now.getHours();
    let Min = now.getMinutes() < 10 ? "0" + now.getMinutes() : now.getMinutes();
    return `${M}-${D} ${H}:${Min}`;
};

/* 
  res:15:16
*/
cjs.timeStyleSeven = function (time) {
    let now = new Date(time);
    let H = now.getHours() < 10 ? "0" + now.getHours() : now.getHours();
    let Min = now.getMinutes() < 10 ? "0" + now.getMinutes() : now.getMinutes();
    return `${H}:${Min}`;
};

/* 
  res:下午3:16
*/
cjs.timeStyleEight = function (time) {
    let now = new Date(time);
    let H = now.getHours() < 10 ? "0" + now.getHours() : now.getHours();
    let Min = now.getMinutes() < 10 ? "0" + now.getMinutes() : now.getMinutes();
    let Sec = now.getSeconds() < 10 ? "0" + now.getSeconds() : now.getSeconds();
    if (H >= 12) {
        H = H % 12;
        return `下午${H}:${Min}`;
    } else {
        return `上午${H}:${Min}`;
    }
};

/* 
  res:深夜8:30
*/
cjs.timeStyleNine = function (time) {
    let now = new Date(time);
    let H = now.getHours() < 10 ? "0" + now.getHours() : now.getHours();
    let Min = now.getMinutes() < 10 ? "0" + now.getMinutes() : now.getMinutes();
    if (0 <= H && H <= 4) {
        return `凌晨 ${H}:${Min}`;
    } else if (5 <= H && H <= 7) {
        return `早晨 ${H}:${Min}`;
    } else if (8 <= H && H <= 10) {
        return `上午 ${H}:${Min}`;
    } else if (11 <= H && H <= 12) {
        return `中午 ${H}:${Min}`;
    } else if (13 <= H && H <= 16) {
        return `下午 ${H % 12}:${Min}`;
    } else if (17 <= H && H <= 19) {
        return `傍晚 ${H % 12}:${Min}`;
    } else if (20 <= H && H <= 22) {
        return `晚上 ${H % 12}:${Min}`;
    } else {
        return `深夜 ${H % 12}:${Min}`;
    }
};
// ***************************************处理时间日期-end***************************

/*
 * @description: 充值接口,步骤首先是预支付,然后支付(即充值)
 * @param {onject} result或者其他形参
 * @return: ''
 */

// 充值接口
cjs.wxRecharge = function (result) {
    const that = this;
    wx.login({
        success: (e) => {
            if (e.code) {
                wx.request({
                    url: `${that.globalData.serverUrl}api/login/getopenid?code=${e.code}`, //给后台传code以换取openid,注意:appid必须是商家的
                    success: function (res) {
                        if (res.statusCode == 200) {
                            wx.request({
                                url: `${that.globalData.serverUrl}api/Wxrecharge/prepay`, //预支付,给后台openid换取数据,部分数据在支付接口用得着
                                method: "post",
                                data: {
                                    openid: res.data.data.openid, //openid
                                    attach: result.userid, //用户id
                                    order_sn: 2019041311278213891, //商户订单号,可以从嗨嗨获取也可以本地生成
                                    total_fee: result.fee, //金额
                                    body: result.body //其他参数,例如:SPA-钱包充值
                                },
                                success: function (e) {
                                    var rs = e.data.data
                                    // 判断条件根据后台数据进行填写
                                    if (e.statusCode == 200) {
                                        wx.request({
                                            url: `${that.globalData.serverUrl}api/Wxrecharge/pay`, //支付接口
                                            method: "post",
                                            data: {
                                                prepay_id: e.data.data.data.prepay_id //预支付成功之后的获取到的prepay_id
                                            },
                                            success: function (e) {
                                                var res = e.data.data.details
                                                wx.requestPayment({
                                                    timeStamp: res.timeStamp.toString(), //后台传来的时间戳
                                                    nonceStr: res.nonceStr, //随机字符串,后台传来的
                                                    package: res.package, //数据包,后台传来的
                                                    signType: 'MD5', //默认
                                                    paySign: res.paySign, //签名,后台传来的
                                                    success(res) {
                                                        if (res.errMsg == "requestPayment:ok") {
                                                            wx.showToast({
                                                                title: '支付成功!',
                                                            });
                                                            // 然后进行充值成功后的操作,可以跳转页面之类的,,
                                                        }
                                                    },
                                                    fail(res) {
                                                        wx.showToast({
                                                            title: '取消支付',
                                                            icon: "none"
                                                        })
                                                    },
                                                })
                                            }
                                        })
                                    }
                                }
                            })
                        } else {
                            console.log(res.errMsg)
                        }
                    },
                })
            }
        }
    })
}
// ===================================================================================================
// FANZHEN                                        
// ===================================================================================================

// ===================================================================================================
// BAOXIN                                        
// ===================================================================================================

// ===================================================================================================
// SHENGJIE                                        
// ===================================================================================================

// ===================================================================================================
// XUEKUN                                        
// ===================================================================================================

// ===================================================================================================
// YONGHAO
// ===================================================================================================

// ===================================================================================================
// CHANGSHUN                                        
// ===================================================================================================

// ===================================================================================================
// HAOYU                                        
// ===================================================================================================
module.exports = cjs;